<template>
  <div class="pull-height" :style="{backgroundImage:'url('+login_center_bg+')'}" style="background-size:100% 100%">
    <el-card class="login-form-layout">
      <el-form autoComplete="on"
               id="loginForm"
               :model="loginForm"
               :rules="loginRules"
               ref="loginForm"
               label-position="left">
        <h2 class="login-title color-main">电影票预定微服务管理平台</h2>
        <el-form-item prop="username" label="用户名" class="login-label">
          <el-col :span="16">
            <el-input name="username"
                      v-model="loginForm.username"
                      autoComplete="on"
                      label-width="auto"
                      placeholder="请输入用户名">
              <!--                        <span slot="prefix">-->
              <!--                          <svg-icon icon-class="password" class="color-main"></svg-icon>-->
              <!--                        </span>-->
            </el-input>
          </el-col>
          <!--          <span slot="prefix">-->
          <!--              <svg-icon icon-class="user" class="color-main"></svg-icon>-->
          <!--            </span>-->
        </el-form-item>
        <el-form-item prop="password" label="密  码" class="login-label">
          <el-col :span="16">
            <el-input name="password"
                      :type="pwdType"
                      @keyup.enter.native="handleLogin"
                      v-model="loginForm.password"
                      autoComplete="on"
                      label-width="auto"
                      placeholder="请输入密码">
              <!--                        <span slot="prefix">-->
              <!--                          <svg-icon icon-class="password" class="color-main"></svg-icon>-->
              <!--                        </span>-->
              <span slot="suffix" @click="showPwd">
                          <svg-icon icon-class="eye" class="color-main"></svg-icon>
                        </span>
            </el-input>
          </el-col>
        </el-form-item>
        <el-form-item style="margin-bottom: 20px;text-align: center">
          <el-button style="width: 45%" type="primary" :loading="loading" @click.native.prevent="handleLogin">
            登录
          </el-button>
          <p></p>
          <el-button style="width: 45%" type="primary" @click.native.prevent="userRegister">
            注册
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!--    <img :src="login_center_bg" class="login-center-layout">-->
  </div>
</template>

<script>
import {isValidUsername} from '@/utils/validate';
import {getCookie, setCookie} from '@/utils/support';

import login_center_bg from '@/assets/images/movie.jpeg'

export default {
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isValidUsername(value)) {
        callback(new Error('请输入正确的用户名'));
      } else {
        callback();
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value === undefined || value === null) {
        callback(new Error("密码不能为空"));
      } else if (value.length < 6) {
        callback(new Error('密码不能小于6位'));
      } else {
        callback();
      }
    };
    return {
      userList: [],
      loginForm: {
        username: 'guest',
        password: '123456',
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validateUsername}],
        password: [{required: true, trigger: 'blur', validator: validatePass}]
      },
      loading: false,
      pwdType: 'password',
      login_center_bg,
      dialogVisible: false,
      supportDialogVisible: false,
    }
  },

  created() {
    this.loginForm.username = getCookie("username");
    this.loginForm.password = getCookie("password");
  },
  mounted() {
    if (this.loginForm.username === undefined || this.loginForm.username === '') {
      this.loginForm.username = 'guest'
      this.loginForm.password = '123456'
      this.handleLogin()
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          // let isSupport = getSupport();
          // if(isSupport===undefined||isSupport==null){
          //   this.dialogVisible =true;
          //   return;
          // }
          this.loading = true;
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false;
            setCookie("username", this.loginForm.username, 15);
            setCookie("password", this.loginForm.password, 15);
            this.$router.push({path: '/fms'})
            // location.reload()
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('参数验证不合法！');
          return false
        }
      })
    },
    userRegister() {
      this.$router.push("register");
    }
  }
}
</script>
<style>
#loginForm .el-form-item__label {
  color: white;
  width: 80px;
}
</style>
<style scoped>
.pull-height {
  width: 100vw;
  height: 100vh;
}

.login-form-layout {
  position: absolute;
  left: 0;
  right: 0;
  width: 360px;
  margin: 140px auto;
  border: none;
  background: rgba(0, 0, 0, .65);
}

.login-title {
  text-align: center;
}

.login-center-layout {
  background: #409EFF;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  margin-top: 200px;
}

</style>
